/**
 *
 * Copyright 2015 RSVP Technologies Inc. All rights reserved.
 * ReGeoCoding.java
 *
 */
package com.gfinal.map.geo;

import java.util.ArrayList;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gfinal.map.common.Constant;
import com.gfinal.map.util.HttpUtils;
import com.gfinal.map.util.Print;

/**
 * @author Gao Yudong (gyd@nbicc.com)
 * @date 2015-2-26
 */
public class ReGeoCoding {
	private static final String TAG = ReGeoCoding.class.getName();

	private float lat = 0;
	private float lng = 0;
	/*
	 * 是否显示附近100米范围内的POI 0-不显示 1-显示
	 */
	private int pois = 0;

	public void setLat(float lat) {
		this.lat = lat;
	}
	public void setLng(float lng) {
		this.lng = lng;
	}
	public void setPois(int pois) {
		this.pois = pois;
	}

	@SuppressWarnings({"rawtypes", "unchecked"})
	private String createURI() {
		ArrayList params = new ArrayList();
		params.add(new BasicNameValuePair("ak", Constant.BAIDU_AK));
		params.add(new BasicNameValuePair("output", "json"));
		params.add(new BasicNameValuePair("coordtype", "bd09ll"));
		params.add(new BasicNameValuePair("pois", String.valueOf(pois)));
		params.add(new BasicNameValuePair("location", String.valueOf(lat) + ","
				+ String.valueOf(lng)));
		return Constant.GEOCODING_URL + "?"
				+ URLEncodedUtils.format(params, Constant.CHARSET);
	}

	public GeoBean execute() {
		GeoBean geo = null;
		String response = HttpUtils.doGet(createURI());
		JSONObject obj = JSON.parseObject(response);
		if (obj.getIntValue("status") == 0) {
			JSONObject result = obj.getJSONObject("result");
			geo = JSON.parseObject(result.toJSONString(), GeoBean.class);
		}
		return geo;
	}
	
	public static void main(String[] args) {
		ReGeoCoding search = new ReGeoCoding();
		search.setLat(29.875964f);
		search.setLng(121.560824f);
		search.setPois(1);
		GeoBean geo = search.execute();
		Print.pln(TAG, geo);
	}
}
